package icu.ac.kr.simpleRssReader.activity;

import icu.ac.kr.simpleRssReader.*;
import icu.ac.kr.simpleRssReader.network.Communication;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ChannelAdd extends Activity {

	static final String TAG = "ChannelAdd";
	ProgressDialog progress;
	Handler handler = new Handler();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.channeladd);
 	
		Button btn = (Button) findViewById(R.id.channeladd_btn);
 
		btn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub

				addSite();
						
			}
			
		});
		
	}

	private void addSite() {
		
			
		final EditText edit = (EditText) findViewById(R.id.channeladd_edit);
		
		progress = ProgressDialog.show(ChannelAdd.this, "Downloading", "Adding new Rssreader Site", true, false);
		 
		handler.postDelayed(new Runnable(){
		
			public void run(){
				
				String url = edit.getText().toString();
				
				Communication com = new Communication(getContentResolver());

				try{
				
				com.downloadXML(url);
				Log.d(TAG, "Download Success");
				progress.dismiss();
				finish();
				}catch(Exception ie)
				{
					Log.d(TAG, "Download Failed");
		   		    progress.dismiss();
					new AlertDialog.Builder(ChannelAdd.this).setTitle("Network Error").setMessage("I am sorry").setPositiveButton("ok", null).show();
					finish();
				}
			}
			
		}, 1000);
		
		 		
	}

	


}
